﻿using Entity;
using IRealize;
using Microsoft.Data.SqlClient;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Realize
{
    public class HF : IHF
    {
        public async Task<List<Human_File>> Server(string sqlserver)
        {
            Task<List<Human_File>> task = Task<List<Human_File>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from human_file where check_status = 1";
                    return con.Query<Human_File>(sql).ToList();
                }
            });
            return await task;
        }

        public async Task<List<Human_File>> Server2(string kf_id, string? sk_id, string? tk_id, string sqlserver)
        {
            Task<List<Human_File>> task = Task<List<Human_File>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from human_file where check_status = 1 and first_kind_id = '{kf_id}' ";
                    if (sk_id != null && sk_id != "")
                        sql += $"and second_kind_id = '{sk_id}'";
                    if (tk_id != null && tk_id != "")
                        sql += $"and third_kind_id = '{tk_id}'";
                    return con.Query<Human_File>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<List<Human_File>> Server3(string h_id,string sqlserver)
        {
            Task<List<Human_File>> task = Task<List<Human_File>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from human_file where human_id = '{h_id}'";
                    return con.Query<Human_File>(sql).ToList();
                }
            });
            return await task;
        }

        public async Task<int> Update(string h_id,decimal ps_sum, string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {
                string sql = $@"update human_file set check_status = 2 , paid_salary_sum={ps_sum}
                  where human_id = '{h_id}'";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
        public async Task<int> Update2(string h_id,decimal ps_sum, string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {
                string sql = $@"update human_file set paid_salary_sum={ps_sum}
                  where human_id = '{h_id}'";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
